<template>
  <div class="container">
    <el-card class="box-card">
      <!-- 标题 -->
      <div slot="header" class="clearfix">
        <span>{{form.id?'试题修改':'试题录入'}}</span>
      </div>
      <!-- 表单 -->
      <el-form ref="form" :model="form" :rules="rules">
        <el-form-item label="学科：" label-width="100px" prop="subjectID">
          <el-select
            v-model="form.subjectID"
            placeholder="请选择"
            @change="subjectChange(form.subjectID)"
          >
            <el-option
              v-for="item in subjectList"
              :key="item.id"
              :label="item.subjectName"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="目录：" label-width="100px" prop="catalogID">
          <el-select v-model="form.catalogID" placeholder="请选择">
            <el-option
              v-for="item in cataList"
              :key="item.id"
              :label="item.directoryName"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="企业：" label-width="100px" prop="enterpriseID">
          <el-select v-model="form.enterpriseID" placeholder="请选择">
            <el-option
              v-for="item in priseList"
              :key="item.id"
              :label="item.company"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="城市：" label-width="100px" prop="city">
          <el-select
            v-model="form.province"
            placeholder="请选择"
            class="city-select"
            @change="cityChange"
          >
            <el-option
              v-for="(item, index) in provList"
              :label="item"
              :key="index"
              :value="item"
            ></el-option>
          </el-select>
          <el-select
            v-model="form.city"
            placeholder="请选择"
            class="city-select"
          >
            <el-option
              v-for="(item, index) in cityList"
              :label="item"
              :key="index"
              :value="item"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="方向：" label-width="100px" prop="direction">
          <el-select v-model="form.direction" placeholder="请选择">
            <el-option v-for="(item,index) in direction" :key="index" :label="item" :value="item"></el-option>
            </el-select>
        </el-form-item>
        <el-form-item label="题型：" label-width="100px" prop="questionType">
          <el-radio-group v-model="form.questionType">
            <el-radio :label="'1'">单选</el-radio>
            <el-radio :label="'2'">多选</el-radio>
            <el-radio :label="'3'">简答</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="难度：" label-width="100px" prop="difficulty">
          <el-radio-group v-model="form.difficulty">
            <el-radio :label="'1'">简单</el-radio>
            <el-radio :label="'2'">一般</el-radio>
            <el-radio :label="'3'">困难</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="题干：" label-width="100px" prop="question">
          <quill-editor
            v-model="form.question"
            :options="editorOption"
          >
          </quill-editor>
        </el-form-item>
        <!-- 单选 -->
        <radiobox v-if="form.questionType === '1'" :value="form.options" @input="radioSel"></radiobox>
        <!-- 多选 -->
        <check v-if="form.questionType === '2'" :value="form.options" :id='form.id' @input="checkSel"></check>
        <el-form-item
          label="解析视频："
          label-width="100px"
          prop="videoURL"
        >
          <el-input v-model="form.videoURL"></el-input>
        </el-form-item>
        <el-form-item
          label="答案解析："
          label-width="100px"
          prop="answer"
        >
          <quill-editor v-model="form.answer" :options="editorOption"> </quill-editor>
        </el-form-item>
        <el-form-item label="题目形式：" label-width="100px">
          <el-input type="textarea" v-model="form.remarks"></el-input>
        </el-form-item>
        <el-form-item label="试题标签：" label-width="100px">
          <el-select v-model="form.tags" multiple placeholder="请选择">
            <el-option v-for="item in tagList" :key="item.id" :label="item.tagName" :value="item.tagName"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button class="button" type="primary" @click="confirm"
            >{{form.id?'确认修改':'确认提交'}}</el-button
          >
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script>
import Radiobox from './components/Radio.vue'
import Check from './components/Check.vue'
// 学科
import { list } from '@/api/hmmm/subjects'
// 方向
import { list as Dlist } from '@/api/hmmm/directorys.js'
// 企业
import { list as Clist } from '@/api/hmmm/companys.js'
//  城市 地区 静态 枚举
import { provinces, citys } from '@/api/hmmm/citys.js'
//  静态 枚举  方向
import { direction } from '@/api/hmmm/constants'
// 标签
import { list as Tlist } from '@/api/hmmm/tags.js'
//
import { add, detail, update } from '@/api/hmmm/questions.js'
// /^https:\/\/[^0]*.mp4/
const url = /^(https|http|ftp|rtsp|mms):\/\/[^0]*/
export default {
  name: 'question-new',
  components: {
    Radiobox,
    Check
  },
  data () {
    var checkURL = (rule, value, callback) => {
      if (!url.test(value) && value) {
        callback(new Error('请输入正确的地址'))
      } else {
        callback()
      }
    }
    return {
      // 富文本 配置
      editorOption: {
        modules: {
          toolbar: {
            container: [
              ['bold', 'italic', 'underline', 'strike'],
              [{ list: 'ordered' }, { list: 'bullet' }, 'blockquote'],
              ['code-block', 'image', 'link']
            ]
          }
        }
      },
      radio: '',
      form: {
        id: null, // 是否为修改
        subjectID: '', // 学科
        catalogID: '', // 目录
        enterpriseID: '', // 企业
        province: '', // 地区
        city: '', // 城市
        direction: '', // 方向
        questionType: '1', // 题型
        difficulty: '1', // 难度
        question: '', // 题干
        options: [], // 选项  code 选项 title 标题  img 图片url isRight 正确答案
        videoURL: '', //  解析视频
        answer: '', // 答案解析
        remarks: '', // 题目备注
        tags: '', // 标签
        isPerfect: '' // 是否京选题
      },
      rules: {
        subjectID: [{ required: true, trigger: ['blur'], message: '不能为空' }],
        catalogID: [{ required: true, trigger: ['blur'], message: '不能为空' }],
        enterpriseID: [
          { required: true, trigger: ['blur'], message: '不能为空' }
        ],
        province: [{ required: true, trigger: ['blur'], message: '不能为空' }],
        city: [{ required: true, trigger: ['blur'], message: '不能为空' }],
        direction: [{ required: true, trigger: ['blur'], message: '不能为空' }],
        questionType: [
          { required: true, trigger: ['blur'], message: '不能为空' }
        ],
        difficulty: [
          { required: true, trigger: ['blur'], message: '不能为空' }
        ],
        question: [{ required: true, trigger: ['blur'], message: '不能为空' }],
        answer: [{ required: true, trigger: ['blur'], message: '不能为空' }],
        videoURL: [{ validator: checkURL, trigger: ['change', 'blur'], message: '地址格式不正确' }]
      },
      subjectList: [], // 学科列表
      cataList: [], // 目录列表
      priseList: [], // 企业列表
      provinces, // 地区列表
      provList: [],
      citys, // 城市列表
      cityList: [],
      direction, // 方向列表
      tagList: [] // 标签列表
    }
  },
  created () {
    this.form.id = this.$route.query.id
    this.hgetDetail()
  },
  mounted () {
    this.hGetList()
    this.hgetCatList()
    this.provList = this.provinces()
  },
  methods: {
    //  获取学科列表
    async hGetList () {
      const res = await list({ pagesize: 1000 })
      this.subjectList = res.data.items
    },
    // 获取题目详情
    async hgetDetail () {
      if (this.form.id) {
        const res = await detail(this.form)
        this.form = res.data
        // 数据类型 不同 更改类型
        this.form.tags = this.form.tags.split(',')
        this.subjectChange()
      }
    },
    // 获取目录列表
    async hgetCatList () {
      const resq = await Clist({ pagesize: 1000 })
      this.priseList = resq.data.items
    },
    // province 改变后  城市联动
    cityChange () {
      this.cityList = this.citys(this.form.province)
    },
    // subject 改变后 学科联动
    async subjectChange (id) {
      const res = await Dlist({ subjectID: id })
      this.cataList = res.data.items
      const resq = await Tlist({ subjectID: id })
      this.tagList = resq.data.items
    },
    // 提交
    confirm () {
      this.$refs.form.validate(async (bool) => {
        if (bool) {
          this.form.tags = this.form.tags.toString()
          if (this.form.id) {
            await update(this.form)
          } else {
            await add(this.form)
          }
          this.$message.success(`${this.form.id ? '修改' : '录入'}成功`)
          // console.log(res)
          this.$router.push('/questions/list')
        }
      })
    },
    // 单选框 选择后
    radioSel (val) {
      this.form.options = val
    },
    // 多选框 选择后
    checkSel (val) {
      this.form.options = val
    }
  }
}
</script>

<style lang='less' scoped>
.clearfix:before,
.clearfix:after {
  display: table;
  content: '';
}
.clearfix:after {
  clear: both;
}
.button {
  margin-left: 100px;
}
/deep/.ql-editor {
  height: 150px;
}
.box-card {
  margin: 20px 20px;
}
.el-input--medium {
  width: 400px;
}
.el-textarea__inner {
  min-height: 100px;
}
.el-select--medium {
  width: 400px;
}
.city-select {
  width: 200px;
}
.editor {
  width: 400px;
}
</style>
